var fname= null;
var areid = null;
var id= null;
var floor_id = null;
var areaname = null;


var theData = new Vue({
    el: "#wrap",
    data: {
        nowPage: 1,   //当前页码
        totalPage: 0,  //总页码
        paginationItems: [   //页面数组
            /*{page:{}}*/  //数据格式
        ],
        tableItems: [       //表格数组
            /*{info:{}}*/  //数据格式
        ],
        areaI: [//存放获取到的区域信息

        ],
        setUp_user: {},     //配置的用户
        search_btn_remark: "",    //存放用户搜索内容
        token: token,        //input file 的加密
        time: unixtime       //input file 的加密
    },
    methods: {
        getList: function (nextPage) {   //获取每个分页的数据
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/floor/get_floor_list.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    nextPage: nextPage,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0){
                        console.log(res);
                        that.tableItems = res.msg;
                        that.nowPage = nextPage;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        getPagination: function () {    //获取分页数量
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/floor/floor_pag_info.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        console.log(res.msg);
                        that.paginationItems = res.msg;
                        that.totalPage = res.total_page;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        search: function () {      //查询
            var regSpace = /^\s+$/;
            if ($("#search-text").val() && !regSpace.test($("#search-text").val())) {
                this.search_btn_remark = $("#search-text").val();
                this.getPagination();
                this.getList(1);
            } else {
                MyAlert("请输入搜索内容");
            }
        },

        getfloorid:function(){//获取区域信息
            var that = this;
            $.ajax({
                url: 'api/floor/get_area.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        that.areaI = res.msg;
                        console.log( that.areaI);
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },

        popAddFloor:function () {//添加楼宇信息弹框
            $("#pop-add-floor").modal();
        },


        popAddFloorSubmit:function(){//楼宇信息添加提交
            var that = this;
            var floorname = $("#pop-add-floor-name").val();
            var belongarea = $('#pop-add-floor-area option:selected').text();
            if(floorname!="" && belongarea!=""){
                for(var i=0;i<that.areaI.length;i++){//对比选择的区域名称获取区域ID
                    if(belongarea===that.areaI[i].info.area_name){
                        belongarea= that.areaI[i].info.id;
                    }
                }
                $.ajax({
                    url: 'api/floor/add_floor.php',
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        floorname:floorname,
                        arear_id:belongarea,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                window.location.reload();
                            });

                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            }
            else{
                MyAlert("请填写必填信息");
            }
        },


        popUpdataFloor:function(item){//修改楼宇信息弹窗
            $("#pop-updata-floor-name").val(item.info.floor_name);
            $("#pop-old-floor-name").val(item.info.area_name);
            $("#pop-updata-floor").modal();
            area_name = item.info.area_name;
            fname= item.info.floor_name;
            areid = item.info.area_id;
            id= item.info.id;
            floor_id = item.info.floor_id;
        },
        popUpdataFloorSubmit:function(){//修改楼宇信息提交
            var ffname = $("#pop-updata-floor-name").val();
            var area_name= $("#pop-updata-floor-area option:selected").text();
            var are= $("#pop-updata-floor-area").val();
            if(ffname==""||are==""){
                MyAlert("请完善修改信息!");
            }
            else{
                console.log(area_name,areid,id,floor_id,ffname,are);
                MyConfirm("确定修改?", function (){
                    $.ajax({
                        url: "api/floor/updataFloorAuthority.php",
                        type: 'POST',
                        data: {
                            token: token,
                            time: unixtime,
                            floor_name:ffname,
                            area_id:are,
                            id:id,
                            floor_id:floor_id,
                            area_name:area_name,
                        },
                        dataType: 'JSON',
                        beforeSend: function () {
                            $("#loading-mask").show();
                        },
                        error: function () {
                            $("#loading-mask").hide();
                            alert("请求超时");
                        },
                        success: function (res) {
                            $("#loading-mask").hide();
                            if (res.err == 0) {
                                MyAlert(res.msg, function () {
                                    console.log(123);
                                    window.location.reload();
                                });
                            } else {
                                MyAlert(res.msg);
                            }
                        }
                    })
                })
            }
        },

        deleteFloorAuthority:function (index){//删除楼宇信息
            MyConfirm("确定删除?", function () {
                $.ajax({
                    url: "api/floor/deleteFloorInfo.php",
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        index: index
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        alert("请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                console.log(123);
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            })
        },

    }
});

theData.getfloorid();//首次加载获取区域信息
theData.getPagination();   //首次加载总页码
theData.getList(theData.nowPage);  //首次加载首个分页